package com.htc.adapter;

import java.io.File;
import java.util.ArrayList;

import com.htc.constants.Constants;
import com.htc.dlc.R;
import com.htc.model.dto.WellnessSchChildDTO;
import com.htc.model.dto.WellnessSchParentDTO;
import com.htc.utils.CommonActions;
import com.htc.webservice.PicassoUtils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class WellnessSchAdapter extends BaseExpandableListAdapter {
	CommonActions ca;
	private Context context;
	private LayoutInflater inflater;
	private ArrayList<WellnessSchParentDTO> wellnessSchParent;

	public WellnessSchAdapter(Context paramContext, int paramInt,
			ArrayList<WellnessSchParentDTO> paramArrayList) {
		this.context = paramContext;
		this.wellnessSchParent = paramArrayList;
		this.inflater = ((LayoutInflater) paramContext
				.getSystemService("layout_inflater"));
		this.ca = new CommonActions(paramContext);
	}

	public void addItem(WellnessSchChildDTO item, WellnessSchParentDTO group) {

		if (!wellnessSchParent.contains(group)) {

			wellnessSchParent.add(group);

		}

		int index = wellnessSchParent.indexOf(group);

		ArrayList<WellnessSchChildDTO> ch = wellnessSchParent.get(index)
				.getSchChild();

		ch.add(item);

		wellnessSchParent.get(index).setSchChild(ch);

	}

	public Object getChild(int groupPosition, int childPosititon) {
		ArrayList<WellnessSchChildDTO> chList = wellnessSchParent.get(
				groupPosition).getSchChild();

		return chList.get(childPosititon);

	}

	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	public View getChildView(int groupPosition, final int childPosition,
			boolean isLastChild, View convertView, ViewGroup paramViewGroup) {
		ArrayList<WellnessSchChildDTO> arrData = ((WellnessSchParentDTO) wellnessSchParent
				.get(groupPosition)).getSchChild();

		if (convertView == null) {
			convertView = this.inflater.inflate(
					R.layout.fragment_wellness_schedule_childrow, null);
		}
		TextView txtTitle = (TextView) convertView
				.findViewById(R.id.tv_wellness_sch_title);
		txtTitle.setTypeface(this.ca.getRailway_Regular());
		RelativeLayout localRelativeLayout = (RelativeLayout) convertView
				.findViewById(R.id.rl_wellnesssch);
		TextView txtnorecore = (TextView) convertView
				.findViewById(R.id.tv_norecord);
		TextView txttime = (TextView) convertView
				.findViewById(R.id.tv_wellness_sch_time);
		txttime.setTypeface(this.ca.getTrebuchet_MS());
		ImageView localImageView = (ImageView) convertView
				.findViewById(R.id.iv_wellness_sch_icon);

		// if (!((ModelWellnessSchChild) localArrayList.get(childPosition))
		// .getWellnessSchTitle().contains(null)) {
		localRelativeLayout.setVisibility(0);
		txtnorecore.setVisibility(8);
		String str = ((WellnessSchChildDTO) arrData.get(childPosition))
				.getWellnessSchTime();
		if ((str.length() == 8) || (str.length() == 7)) {
			str = str.substring(0, 4);
			if (str.charAt(2) == '.')
				str = str + "0";
		}
		for (int i = 0; i < str.length(); i++) {

			txttime.setText(str);
			txtTitle.setText(((WellnessSchChildDTO) arrData.get(childPosition))
					.getWellnessSchTitle());

			String path = (String) ((WellnessSchChildDTO) arrData
					.get(childPosition)).getWellnessSchImage();
			String currentUrl = Constants.URL_IMAGE_PAHT + path;
			if (path != null) {
				String[] strs = path.split("/");
				File f = new File(Environment.getExternalStorageDirectory()
						+ "/" + Constants.APP_NAME + "/" + strs[0] + "/"
						+ strs[1]);
				if (f.exists()) {
					if (f.length() == 0) {
						f.delete();
						PicassoUtils.getInstance().showImageFromUrl(context,
								currentUrl, R.drawable.bgday, localImageView);
						// TODO: create folder?

						PicassoUtils.getInstance().saveImageFromUrl(
								context.getApplicationContext(), currentUrl,
								strs[1], strs[0]);
					} else {
						Bitmap bmp = BitmapFactory.decodeFile(f
								.getAbsolutePath());
						localImageView.setImageBitmap(bmp);
					}

				} else {
					PicassoUtils.getInstance().showImageFromUrl(context,
							currentUrl, R.drawable.bgday, localImageView);
					// TODO: create folder?

					PicassoUtils.getInstance().saveImageFromUrl(
							context.getApplicationContext(), currentUrl,
							strs[1], strs[0]);
				}
			}

			if (str.length() == 2)
				str = str + ":00";

			if (str.charAt(i) == '.')
				str = str.replace(".", ":");

		}
		return convertView;

	}

	public int getChildrenCount(int groupPosition) {
		ArrayList<WellnessSchChildDTO> chList = wellnessSchParent.get(
				groupPosition).getSchChild();
		return chList.size();

	}

	public Object getGroup(int groupPosition) {
		return wellnessSchParent.get(groupPosition);
	}

	public int getGroupCount() {
		return this.wellnessSchParent.size();
	}

	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	public View getGroupView(int groupPosition, boolean paramBoolean,
			View paramView, ViewGroup paramViewGroup) {
		WellnessSchParentDTO group = (WellnessSchParentDTO) getGroup(groupPosition);
		if (paramView == null)
			paramView = this.inflater.inflate(
					R.layout.fragment_wellness_schedule_parentrow, null);

		TextView localTextView = (TextView) paramView
				.findViewById(R.id.tv_wellnessschedule_parentday);
		localTextView.setTypeface(this.ca.getRailway_Regular());
		localTextView.setText(((WellnessSchParentDTO) this.wellnessSchParent
				.get(groupPosition)).getWellnessSchDay());
		return paramView;
	}

	public boolean hasStableIds() {
		return true;
	}

	public boolean isChildSelectable(int paramInt1, int paramInt2) {
		return true;
	}
}